﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PreContractWeb.Report
{
    public partial class ViewDetail : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Header.Controls.Add(new LiteralControl(@"<base target=""_self"" />"));

            Response.Cache.SetCacheability(HttpCacheability.NoCache);


            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);



            //if (!Page.IsPostBack)
            //{
            //    this.btnExport.Visible = false;
            //    //attach client events
            //    this.btnExport.Attributes.Add("onClick", "RptExportToExcel('" + BEGDC.PreContract.BLL.ReportHelper.ReportType.BlackList.ToString() + "');return false;");

            //}
            
            RunReport(user.EmployeeId);

        }

        protected string SearchSQL
        {
            get
            {
                if (Session["SearchSQL"] == null) return string.Empty;
                return Session["SearchSQL"] as string;
            }
            set
            {
                Session["SearchSQL"] = value;
            }
        }

        private void RunReport(long employeeID)
        {
            string err = string.Empty;
            string reportHTML = string.Empty;
            string excelHTML = string.Empty;
            string filter = string.Empty;
           
            try
            {
                reportHTML = BEGDC.PreContract.BLL.ReportHelperService.GetDetailReportHTML(employeeID,((BEGDC.PreContract.BLL.Authentication.PCPrincipal)HttpContext.Current.User).DataPermission,
                    Page.Request.QueryString,
                    out err,
                    out excelHTML,
                    out filter);

                if (err != string.Empty)
                {
                    this.divReport.InnerHtml = "";
                    this.divError.InnerHtml = err;
                    this.btnExport.Visible = false;
                }
                else
                {
                    this.divReport.InnerHtml = reportHTML;
                    //Session[ReportHelper.ReportType.BlackList.ToString()] = excelHTML;
                    this.divError.InnerHtml = "";

                    this.btnExport.Visible = true;

                    this.SearchSQL = filter;
                    //Response.Write(Page.Request.QueryString);
                    //return;
                    Page.Response.Redirect("../Task/AllPre-Contracts.aspx?Search=2" + "&guid=" + System.Guid.NewGuid().ToString("N"));

                    //this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>var   new_window=window.open('AllPre-Contracts.aspx?Search=1','','top=0,left=0,menubar=0,resizable=1,scrollbars=yes');new_window.resizeTo(screen.availWidth,screen.availHeight); </script>");
                }
            }
            catch(Exception ex)
            {
                
                this.divReport.InnerHtml = "";
                this.btnExport.Visible = false;
                divError.InnerHtml = AlertMessage.GetReportDataError;

                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");

            }
        }
    }
}
